package com.denso.workflow.admin.operator;

import com.denso.workflow.common.model.Operator;
import com.denso.workflow.interceptor.AdminInterceptor;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;

import java.util.List;


@Before(AdminInterceptor.class)
public class OperatorController extends Controller {

    static final OperatorService service = new OperatorService();

    public void index() {
        List<Operator> operatorList = service.find();
        setAttr("operatorList", operatorList);
        render("index.html");
    }

    public void add() {
        render("add.html");
    }

    public void save() {
        Operator operator = getModel(Operator.class);

        List<Operator> result = service.findByName(operator.getName());
        if(result != null && result.size() > 0) {
            // 操作员名称已存在
            setAttr("notice", "操作员名称已注册，请重新修改");
            render("add.html");
            return;
        } else {
            // 保存操作员
            service.save(operator);
            setAttr("notice", "操作员添加成功");
            index();
        }
    }

    public void update() {
        Operator operator = getModel(Operator.class);
        service.update(operator);
        setAttr("notice", "操作员更新成功");
        index();
    }

    public void delete() {
        int id = getParaToInt("id");
        service.deleteById(id);
        setAttr("notice", "操作员删除成功");
        index();
    }
}
